package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.TypeTempService;
import com.dongdongshop.util.Result;
import com.dongdongshop.vo.TemplateVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @DubboReference
    private TypeTempService tts;

    @DubboReference
    private ItemCatService is;

    @DubboReference
    private GoodsService gs;

    @RequestMapping("toAdd")
    public String toAdd(){
        return "admin/goods_edit";
    }


    @RequestMapping("getSelect")
    @ResponseBody
    public Result getSelect(Long parentId){
        try {
            List<TbItemCat> select = is.getSelect(parentId);
            return Result.OK().setData(select);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("getId")
    @ResponseBody
    public Result getId(Long id){
        try {
            TbItemCat id1 = is.getId(id);
            return Result.OK().setData(id1);
        } catch (Exception e) {
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }

    @RequestMapping("getImt")
    @ResponseBody
    public Result getImt(Long id) {
        try {
            TbItemCat c = is.getId(id);
            Long typeId = c.getTypeId();
            TbTypeTemplate type = tts.getById(typeId);
            return Result.OK().setData(type);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("getCustom")
    @ResponseBody
    public Result getCustom(Long itemCatId){
        try {
            //根据itemCatId查询模板数据
            TemplateVO templateVO = tts.getTypeTemplates(itemCatId);
            return Result.OK().setData(templateVO);
        } catch (Exception e) {
            return Result.ERROR();
        }
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbGoods goods, TbGoodsDesc desc,String itemJson) {
        try {
            Subject subject = SecurityUtils.getSubject();
            TbSeller seller = (TbSeller) subject.getPrincipal();
            goods.setSellerId(seller.getSellerId());
            List<TbItem> tbItems = JSONObject.parseArray(itemJson, TbItem.class);
            for (TbItem tbItem : tbItems) {
                tbItem.setSeller(seller.getNickName());
                tbItem.setStatus(seller.getStatus());
                tbItem.setSellerId(seller.getSellerId());
                tbItem.setImage(desc.getItemImages());
                tbItem.setCreateTime(new Date());
                tbItem.setUpdateTime(new Date());
            }
            gs.addGoodsAndDese(goods,desc,tbItems);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR);
        }
    }
}
